﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class ChargeRequest : System.Web.UI.Page
    {
        bool prerender = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
                Response.Redirect("Login.aspx");


        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            RepeaterChargeRequests.DataSource = MidApp.ManagePendingStockRequest.GetPendingStockRequests(Common.GetLoggedUser(this.Page).UserID);
            RepeaterChargeRequests.DataBind();

            if (prerender)
                SetDropDownListPlayer();

            if (DropDownListTeam.Items.Count == 0)
            {
                ButtonConfirm.Enabled = false;
                ButtonConfirm.ToolTip = "Nejste členem žádného týmu. Nelze nahlásit nabití";
                TextBoxChargeRequest.Enabled = false;
                DropDownListTeam.Enabled = false;
            }
        }
        private void SetDropDownListPlayer()
        {
            var aux = MidApp.ManageTeam.GetTeamsWhereUserIsAnyMember(Common.GetLoggedUser(this.Page).UserID);
            foreach (var item in aux)
            {
                DropDownListTeam.Items.Add(item.TeamName);
            }


        }
        protected void ButtonConfirm_Click(object sender, EventArgs e)
        {
            prerender = false;

            var team = MidApp.ManageTeam.GetTeamByTeamName(DropDownListTeam.SelectedItem.ToString()).TeamID;
            var charge = int.Parse(TextBoxChargeRequest.Text);

            MidApp.ManagePendingStockRequest.RequestCreditCharge(Common.GetLoggedUser(this.Page).UserID, team, charge);

        }
        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            prerender = false;

            var linkbtn = sender as LinkButton;
            var aux = int.Parse(linkbtn.CommandArgument);
            MidApp.ManagePendingStockRequest.DeleteRequest(aux);
        }

        //vraci jmeno tymu pro pozadavek k nabiti kreditu
        protected string GetTeamName(object sender)
        {
            var aux = sender as AppLib.PendingStockRequest;

            return aux.Team.TeamName;
        }

        protected string GetTeamAvatar(object sender)
        {
            var aux = (AppLib.PendingStockRequest)sender;

            var team = MidApp.ManageTeam.GetTeamByID(aux.TeamID);

            return team.ImageURL;
        }
        protected string GetTeamID(object sender)
        {
            var aux = (AppLib.PendingStockRequest)sender;

            return aux.TeamID.ToString();
        }

        //obsluha presmerovani na detail tymu
        protected void LinkButtonTeamDetail_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamDetails.aspx");
        }
    }
}